<?php

/**
 * ReservationController
 *
 * @version $Rev:$
 * @license
 */
class Tour_ReservationController extends Zend_Controller_Action
{

    public function init()
    {
        $this->params = $this->getRequest()->getParams();
        $this->model = new Tour_Model_Reservation();
        System_Template::using("userinfo","Default");
        parent::init();
    }

    public function indexAction()
    {
        $id = $this->params['reservation'];
        $status = $this->params['status'];
        if($id > 0){
            $table = new System_Table_Category();
            $tableCity = new System_Table_City();
            $modelTour = new Tour_Model_Tour();
            $data = $modelTour->getTourInfo($id);
            $this->view->data = $data;
            $this->view->listTourType = $table->fetchAll();
            $this->view->listCity = $tableCity->fetchAll();
        }else{
            $this->_redirect("/tour/search/index");
        }
        $this->view->status = $status;
        
    }

    public function saveAction()
    {        
        $tableReservation       = new System_Table_Reservation();
        $modelTour              = new Tour_Model_Tour();
        $tableTourDescription   = new System_Table_Tourdescription();
        $limit                  = $modelTour->getTourLeftLimit($this->params['IDTOUR']);
        $left_limit = (int)$limit - ((int)$this->params['SONGUOILON'] + (int)$this->params['SOTREEM']);
        $status = 1;
        if($left_limit < 0){
            $status = 3;
        }else{
            $arrReservation = array(
                    'title'             =>  $this->params['TIEUDE'],
                    'full_name'         =>  $this->params['TENNGUOIDAT'],
                    'address'           =>  $this->params['DIACHI'],
                    'email'             =>  $this->params['EMAIL'],
                    'phone'             =>  $this->params['DIENTHOAIBAN'],
                    'mobile_phone'      =>  $this->params['DIENTHOAIDIDONG'],
                    'fax'               =>  $this->params['FAX'],
                    'number_of_adult'   =>  (int)$this->params['SONGUOILON'],
                    'number_of_kid'     =>  (int)$this->params['SOTREEM'],
                    'other_request'     =>  $this->params['TENNGUOIDAT'],
                    'id_tour'           =>  $this->params['IDTOUR'],
                    'id_company'        =>  $this->params['IDCOMPANY'],
                    'reservation_date'  =>  date('Y-m-d H-i-s')
                );
            $arrTourDescription = array(
                'left_limit_of_reservation' => $left_limit
            );
            
            try{
                $tableReservation->insert($arrReservation);
            }catch(Exception $ex){ 
                $status = 2 ;
            }
            if($status == 1){
                try{
                    $tableTourDescription->update($arrTourDescription,'id_tour = '.$this->params['IDTOUR']);
                }catch(Exception $ex){
                    $status = 4 ;
                }
            }
        }
        $this->_redirect("/tour/reservation/index/reservation/".$this->params['IDTOUR']."/status/".$status);
    }


}

